<?php
/*
 *搜索接口API
 */
namespace Home\Controller;
use Think\Controller;
class SearchController extends Controller {
	/*
	 *搜索首页
	 *@param string itemname
	 */
	public function index()
	{
			$keywords=I('get.keywords','','strip_tags');
			$page=I('get.page','1','strip_tags');
			$where['itemname']  = array('like', "%$keywords%");
			$where['bn']=array('like',"%$keywords%");
			$where['_logic'] = 'or';
            $map['_complex'] = $where;
            $map['status']=1;
			$count=M('item')->where($map)->count();
			$pagearr= getPageArr($page,C('PAGESIZE'),$count);
			$res=M('item')->where($map)
						  ->order('itemid desc')
                          ->limit($pagearr['start'],$pagearr['pagesize'])
			              ->getField('itemid,itemname,price,itemimg,bn');
			             
		$pagetotal=$pagearr['pagetotal'];

			if (empty($res) || empty($pagetotal)) {
				$result['status']=0;
				$result['msg']='暂无数据';
				echo json_encode($result,JSON_UNESCAPED_UNICODE);
			}else{
				$result['status']=1;
				$result['items']=array_values($res);
				$result['totalpage']=$pagetotal;
				echo json_encode($result,JSON_UNESCAPED_UNICODE);
			}
	}
}

